//
//  XMTableViewCellHeaderView.m
//  XMFriendCircleDemo
//
//  Created by chenming on 2018/1/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMTableViewCellHeaderView.h"

// Model
#import "XMFriendCircleModel.h"

// View
#import "XMCoreTextLabel.h"
#import "XMTableViewCollectionCell.h"

@interface XMTableViewCellHeaderView ()

// 头像
@property (nonatomic, strong) UIImageView *headIcon;
// 名字
@property (nonatomic, strong) UILabel *headName;
// 内容
@property (nonatomic, strong) UILabel *headTitle;
// 容器
@property (nonatomic, strong) UIView *contentsView;
// 时间
@property (nonatomic, strong) UILabel *headTime;
// 评论
@property (nonatomic, strong) UIButton *commentButton;
// 查看全文
@property (nonatomic, strong) UIButton *seeButton;
// 九宫格图片
@property (nonatomic, strong) UICollectionView *collectionView;
// 图片数据源
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

static NSString *const cellID = @"headerView";
static NSString *const TableViewCollectionCell = @"XMTableViewCollectionCell";


@implementation XMTableViewCellHeaderView

+ (instancetype)headerViewWithTableView:(UITableView *)tableView {
    XMTableViewCellHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellID];
    if (!header) {
        header = [[XMTableViewCellHeaderView alloc] initWithReuseIdentifier:cellID];
        header.contentView.backgroundColor = [UIColor whiteColor];
    }
    return header;
}

- (CGFloat)getSectionHeight:(id)model {
    XMFriendCircleModel *sectionModel = (XMFriendCircleModel *)model;
    CGFloat titleHight = [self.headTitle xm_getHeightByWidth:kSCREEN_WIDTH - 70 title:sectionModel.content font:[UIFont systemFontOfSize:14]];
    return sectionModel.sectionsHeight = CGRectGetMaxY(self.headName.frame) + titleHight + 130;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
}

- (void)setModel:(XMFriendCircleModel *)model {
    _model = model;
    [self.dataSource removeAllObjects];
    [self.headIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.icon]] placeholderImage:nil];
    self.headName.text = model.name;
    self.headTitle.text = model.content;
    self.headTime.text = model.time;
    
    // 按照整数/整数丢失精度仍为整数的思想(例如 9/5=1), 显示按钮的行数 = 显示按钮的个数/(每行显示的按钮个数+1) 行数为_titleArr.count / (4+1) 计算高度
    // CGFloat height = (_titleArr.count / (4+1) + 1) * 30;
//    CGFloat height = self.headTitle.intrinsicContentSize.height;
//    if (height > 50) {
//        self.seeButton.hidden = NO;
//    }
    NSLog(@"%@",NSStringFromCGSize(self.headTitle.intrinsicContentSize));
}

- (CGFloat)tableViewHeaderSection:(NSString *)title titleFontSize:(CGFloat)titleFontSize {
    CGFloat height = [title boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH - 70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:titleFontSize]} context:nil].size.height;
    return height;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.headIcon];
        [self.headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(self.contentView).offset(10);
            make.width.height.equalTo(@(40));
        }];
        
        [self.contentView addSubview:self.headName];
        [self.headName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView).offset(10);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.left.mas_equalTo(self.headIcon.mas_right).offset(10);
        }];
        
        [self.contentView addSubview:self.headTitle];
        [self.headTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headName.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.left.mas_equalTo(self.headIcon.mas_right).offset(10);
        }];
    
        [self.contentView addSubview:self.seeButton];
        [self.seeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(self.headIcon.mas_right).offset(10);
            make.size.mas_equalTo(CGSizeMake(40, 20));
        }];
        
        [self.contentView addSubview:self.headTime];
        [self.headTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.seeButton.mas_bottom).offset(10);
            make.left.mas_equalTo(self.headIcon.mas_right).offset(10);
        }];
        
        [self.contentView addSubview:self.commentButton];
        [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.seeButton.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.size.mas_equalTo(CGSizeMake(20, 20));
            make.bottom.equalTo(self.contentView);
        }];
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 重点代码：contentView的4条边和self对齐
            make.top.left.right.bottom.mas_equalTo(self);
        }];
        
        self.model.isShow = NO;
    }
    return self;
}

- (void)headerSpread:(UIButton *)sender {
    self.model.isShow = !self.model.isShow;
    [self.headTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headName.mas_bottom).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.left.mas_equalTo(self.headIcon.mas_right).offset(10);
    }];
}

/*
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *basecell = nil;
    XMTableViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TableViewCollectionCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor xm_randomColor];
    [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:self.dataSource[indexPath.item]] placeholderImage:nil];
    basecell = cell;
    return basecell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat itemWidth = (kSCREEN_WIDTH - 130) / 3;
    CGFloat itewHight = itemWidth;
    return CGSizeMake(itemWidth, itewHight);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return (section == 2) ? 10 : 0;
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;        //注册
        [_collectionView registerClass:[XMTableViewCollectionCell class] forCellWithReuseIdentifier:TableViewCollectionCell];
    }
    return _collectionView;
}
*/

- (UIButton *)commentButton {
    if (!_commentButton) {
        _commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentButton.backgroundColor = [UIColor whiteColor];
        [_commentButton setImage:[UIImage imageNamed:@"AlbumOperateMore"] forState:UIControlStateNormal];
        [_commentButton setImage:[UIImage imageNamed:@"AlbumOperateMore"] forState:UIControlStateSelected];
        [_commentButton setImage:[UIImage imageNamed:@"AlbumOperateMoreHL"] forState:UIControlStateHighlighted];
    }
    return _commentButton;
}

- (UIButton *)seeButton {
    if (!_seeButton) {
        _seeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _seeButton.backgroundColor = [UIColor xm_randomColor];
        [_seeButton setTitle:@"全文" forState:UIControlStateNormal];
        [_seeButton setTitleColor:[UIColor xm_colorWithHexString:@"556a94" alpha:1] forState:UIControlStateNormal];
        _seeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_seeButton addTarget:self action:@selector(headerSpread:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seeButton;
}

- (UILabel *)headTime {
    if (!_headTime) {
        _headTime = [[UILabel alloc] init];
        _headTime.font = [UIFont systemFontOfSize:12];
        _headTime.textColor = [UIColor xm_colorWithHexString:@"999999" alpha:1];
        _headTime.backgroundColor = [UIColor whiteColor];
        _headName.clipsToBounds = YES;
    }
    return _headTime;
}

- (UIView *)contentsView {
    if (!_contentsView) {
        _contentsView = [[UIView alloc] init];
        _contentsView.backgroundColor = [UIColor xm_randomColor];
    }
    return _contentsView;
}

- (UILabel *)headTitle {
    if (!_headTitle) {
        _headTitle = [[UILabel alloc] init];
        _headTitle.font = [UIFont systemFontOfSize:14];
        _headTitle.textColor = [UIColor xm_colorWithHexString:@"333333" alpha:1];
        _headTitle.backgroundColor = [UIColor whiteColor];
        _headName.clipsToBounds = YES;
    }
    return _headTitle;
}

- (UILabel *)headName {
    if (!_headName) {
        _headName = [[UILabel alloc] init];
        _headName.font = [UIFont boldSystemFontOfSize:14];
        _headName.textColor = [UIColor xm_colorWithHexString:@"556a94" alpha:1];
        _headName.clipsToBounds = YES;
        _headName.backgroundColor = [UIColor whiteColor];
    }
    return _headName;
}

- (UIImageView *)headIcon {
    if (!_headIcon) {
        _headIcon = [[UIImageView alloc] init];
        _headIcon.contentMode = UIViewContentModeScaleAspectFill;
        _headIcon.clipsToBounds = YES;
        _headIcon.backgroundColor = [UIColor xm_colorWithHexString:@"ecf0f4" alpha:1];
    }
    return _headIcon;
}

- (void)setFrame:(CGRect)frame {
    CGRect sectionRect = [self.tableView rectForSection:self.section];
    CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
    [super setFrame:newFrame];
}

@end
